/* 
 * File:   Lesson13.cpp
 * Author: jose
 * 
 * Created on March 29, 2012, 9:27 PM
 */

#include "Lesson13.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <sstream>
#include "SdlUtils.h"
#include "Timer.h"



Lesson13::Lesson13() {
}

Lesson13::Lesson13(const Lesson13& orig) {
}

Lesson13::~Lesson13() {
}

bool  Lesson13::init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Initialize SDL_ttf
    if( TTF_Init() == -1 )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Timer Test", NULL );

    //If everything initialized fine
    return true;
}

bool Lesson13::load_files()
{
    //Load the background image
    background = SdlUtils::loadImageColorKey( "blackbackground.png" );

    //Open the font
    font = TTF_OpenFont( "lazy.ttf", 24 );

    //If there was a problem in loading the background
    if( background == NULL )
    {
        return false;
    }

    //If there was an error in loading the font
    if( font == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void  Lesson13::clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( background );
    SDL_FreeSurface( startStop );
    SDL_FreeSurface( pauseMessage );

    //Close the font
    TTF_CloseFont( font );

    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}

int Lesson13::Run(){
    
     //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //Make the timer
    Timer myTimer;

    //Generate the message surfaces
    startStop = TTF_RenderText_Solid( font, "Press S to start or stop the timer", textColor );
    pauseMessage = TTF_RenderText_Solid( font, "Press P to pause or unpause the timer", textColor );

    //Start the timer
    myTimer.start();

    //While the user hasn't quit
    while( quit == false )
    {
        //While there's an event to handle
        while( SDL_PollEvent( &event ) )
        {
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {
                //If s was pressed
                if( event.key.keysym.sym == SDLK_s )
                {
                    //If the timer is running
                    if( myTimer.is_started() == true )
                    {
                        //Stop the timer
                        myTimer.stop();
                    }
                    else
                    {
                        //Start the timer
                        myTimer.start();
                    }
                }
                //If p was pressed
                if( event.key.keysym.sym == SDLK_p )
                {
                    //If the timer is paused
                    if( myTimer.is_paused() == true )
                    {
                        //Unpause the timer
                        myTimer.unpause();
                    }
                    else
                    {
                        //Pause the timer
                        myTimer.pause();
                    }
                }
            }

            //If the user has Xed out the window
            else if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        //Apply the background
        SdlUtils::applySurface( 0, 0, background, screen );

        //Apply the messages
        SdlUtils::applySurface( ( SCREEN_WIDTH - startStop->w ) / 2, 200, startStop, screen );
        SdlUtils::applySurface( ( SCREEN_WIDTH - pauseMessage->w ) / 2, 250, pauseMessage, screen );

        //The timer's time as a string
        std::stringstream time;

        //Convert the timer's time to a string
        time << "Timer: " << myTimer.get_ticks() / 1000.f;

        //Render the time surface
        seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );

        //Apply the time surface
        SdlUtils::applySurface( ( SCREEN_WIDTH - seconds->w ) / 2, 0, seconds, screen );

        //Free the time surface
        SDL_FreeSurface( seconds );

        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
    }

    //Clean up
    clean_up();

    return 0;
    
}
